package com.exrobot.module.rbt.controller.admin.memberaddress;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.memberaddress.vo.*;
import com.exrobot.module.rbt.dal.dataobject.memberaddress.MemberAddressDO;
import com.exrobot.module.rbt.service.memberaddress.MemberAddressService;

@Tag(name = "管理后台 - 客户地址信息")
@RestController
@RequestMapping("/rbt/member-address")
@Validated
public class MemberAddressController {

    @Resource
    private MemberAddressService memberAddressService;

    @PostMapping("/create")
    @Operation(summary = "创建客户地址信息")
    @PreAuthorize("@ss.hasPermission('rbt:member-address:create')")
    public CommonResult<Long> createMemberAddress(@Valid @RequestBody MemberAddressSaveReqVO createReqVO) {
        return success(memberAddressService.createMemberAddress(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户地址信息")
    @PreAuthorize("@ss.hasPermission('rbt:member-address:update')")
    public CommonResult<Boolean> updateMemberAddress(@Valid @RequestBody MemberAddressSaveReqVO updateReqVO) {
        memberAddressService.updateMemberAddress(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户地址信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:member-address:delete')")
    public CommonResult<Boolean> deleteMemberAddress(@RequestParam("id") Long id) {
        memberAddressService.deleteMemberAddress(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客户地址信息")
                @PreAuthorize("@ss.hasPermission('rbt:member-address:delete')")
    public CommonResult<Boolean> deleteMemberAddressList(@RequestParam("ids") List<Long> ids) {
        memberAddressService.deleteMemberAddressListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户地址信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:member-address:query')")
    public CommonResult<MemberAddressRespVO> getMemberAddress(@RequestParam("id") Long id) {
        MemberAddressDO memberAddress = memberAddressService.getMemberAddress(id);
        return success(BeanUtils.toBean(memberAddress, MemberAddressRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户地址信息分页")
    @PreAuthorize("@ss.hasPermission('rbt:member-address:query')")
    public CommonResult<PageResult<MemberAddressRespVO>> getMemberAddressPage(@Valid MemberAddressPageReqVO pageReqVO) {
        PageResult<MemberAddressDO> pageResult = memberAddressService.getMemberAddressPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberAddressRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户地址信息 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:member-address:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMemberAddressExcel(@Valid MemberAddressPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberAddressDO> list = memberAddressService.getMemberAddressPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户地址信息.xls", "数据", MemberAddressRespVO.class,
                        BeanUtils.toBean(list, MemberAddressRespVO.class));
    }

}